# from selenium import webdriver
from common import setup
from utils.read_excel import read_excel
# class Login:
#     # 初始化操作，得到driver对象
#     def __init__(self,driver):
#         self.driver = driver
#
#     # 填写用户名
#     def username(self,username):
#         self.driver.find_element_by_id('username').clear()
#         self.driver.find_element_by_id('username').send_key(username)
#
#
#     # 填写密码
#     def password(self,password):
#         self.driver.find_element_by_id('password').clear()
#         self.driver.find_element_by_id('password').send_key(password)
#
#
#     # 点击登录
#     def login(self):
#         self.driver.find_element_by_id('login').click()
#
#
#     # 整合登录方法
#     def login_now(self,username,password):
#         self.username()
#         self.password()
#         self.login()
#         return self.driver



# 优化后
class Login:
    # 初始化操作，得到driver对象
    def set_driver(self,driver):
        self.driver = driver






    # 填写用户名
    def username(self,username):

        self.driver.find_element_by_name('userName').clear()
        self.driver.find_element_by_name('userName').send_keys(username)


    # 填写密码
    def password(self,password):
        self.driver.find_element_by_name('userPass').clear()
        self.driver.find_element_by_name('userPass').send_keys(password)


    # 点击登录
    def login(self):
        self.driver.find_element_by_class_name('modal-footer').click()


    def login_now(self,username,password):
        self.username(username)
        self.password(password)
        self.login()

    def open_module(self, text2, text3):
        # start = Login()
        # self.driver = start.login_fast(text1)
        self.driver.find_element_by_link_text(text2).click()
        self.driver.implicitly_wait(5)
        self.driver.find_element_by_link_text(text3).click()
        self.driver.implicitly_wait(3)


    # 封装登录得方法，用于测试用例调用
    def login_user(self,role):
        st = setup.Setup()
        driver = st.driver     # 通过对象获取其driver属性
        self.set_driver(driver)    # 通过调用方法将driver对象变成全局的实例变量
        s = read_excel().read_it('../data/login_info.xlsx')
        # 取得sheet表的所有行数
        for i in range(s.nrows):
            # 取得sheet表的所有列数
            # print(s.cell(i,1).value)
            if s.cell(i, 0).value == role:
                username = s.cell(i, 1).value
                password = s.cell(i, 2).value
                print(username)
                print(password)
                self.login_now(username,password)
        return self.driver
if __name__ == '__main__':
        start = Login()
        start.login_user('超级管理员')
        start.open_module("财务管理","财务流水")